package com.tuniu.activity;

import java.util.ArrayList;
import java.util.List;

import com.android.volley.VolleyError;
import com.tuniu.R;
import com.tuniu.R.id;
import com.tuniu.R.layout;
import com.tuniu.data.interesting.Content;
import com.tuniu.data.interesting.Data;
import com.tuniu.data.interesting.DestRecommend;
import com.tuniu.data.interesting.DestinationTatal;
import com.tuniu.utils.Constants;
import com.tuniu.utils.MyAPIUtils;
import com.tuniu.utils.MyAdapter;
import com.xinbo.utils.GsonUtils;
import com.xinbo.utils.HTTPUtils;
import com.xinbo.utils.ResponseListener;
import com.xinbo.utils.UILUtils;

import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.text.TextUtils;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.GridView;
import android.widget.ImageView;
import android.widget.TextView;
import android.widget.Toast;

/**
 * A simple {@link Fragment} subclass.
 *
 */
public class InterestFragment extends Fragment {

	private MyAdapter myAdapter;

	private ArrayList<Content> contentList = new ArrayList<Content>();

	private LayoutInflater inflater;

	private View view;

	private int pagerPosition;

	public InterestFragment() {
		
	}
	public InterestFragment(int position) {
		this.pagerPosition = position;
		
	}

	@Override
	public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
		this.inflater = inflater;
		if(view==null){
			view = inflater.inflate(R.layout.fragment_interest, container, false);
			initUI();
			initData();
		}
		return view;
	}

	private void initUI() {
		GridView mGridView = (GridView) view.findViewById(R.id.gridView);
		myAdapter = new MyAdapter() {

			private Content content;
			@Override
			public View getView(int position, View convertView, ViewGroup parent) {
				View view =null;
				if(convertView==null){
					 view = inflater.inflate(R.layout.item_gridview, null);
				}else {
					view =convertView;
				}
				ImageView mImageView = (ImageView) view.findViewById(R.id.imagUrl);
				TextView peopleNum = (TextView) view.findViewById(R.id.peopleNum);
				TextView title = (TextView) view.findViewById(R.id.title);
				if(pagerPosition==0){
					content = contentList.get(position);
				}else if(pagerPosition==1){
					
					content = contentList.get(position+4);
				}else if(pagerPosition==2){
					
					content = contentList.get(position+8);
				}
				
				UILUtils.displayImage(content.getImgUrl(), mImageView);
				title.setText(content.getTitle());
				peopleNum.setText(content.getPeopleNum()+"");
				return view;
			}
			@Override
			public int getCount() {
				return contentList.size()/3;
			}
		};

		mGridView.setAdapter(myAdapter);
	}

	
	private void initData() {
		MyAPIUtils.getInterest(getContext(), new ResponseListener() {
			
			@Override
			public void onResponse(String arg0) {
				if (TextUtils.isEmpty(arg0)) {
					Toast.makeText(getContext(), "服务端问题", Toast.LENGTH_SHORT).show();
					return;
				}
				DestinationTatal tatal = GsonUtils.parseJSON(arg0, DestinationTatal.class);
				Data data = tatal.getData();
				DestRecommend destRecommend = data.getDestRecommend();
				List<Content> content = destRecommend.getContent();
				contentList.clear();
				contentList.addAll(content);
				myAdapter.notifyDataSetChanged();				
			}
			
			@Override
			public void onErrorResponse(VolleyError arg0) {
				
			}
		});
	}

}
